//
//  LKManagerAllianceViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/5/26.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKManagerAllianceViewController.h"
#import "SegmentTapView.h"
#import "FlipTableView.h"
#import "LKManagerAlianceListViewController.h"
#import "LKAddShopViewController.h"
#import "LKOpenShopStepOneViewController.h"
#import "LKOpenShopStepOneViewController.h"
#import "LKOpenShopInforDataViewController.h"
#import "LKAllTrendsViewController.h"
#import "LKManagerAllianceSearchViewController.h"
@interface LKManagerAllianceViewController ()<UISearchBarDelegate,FlipTableViewDelegate,SegmentTapViewDelegate,UISearchControllerDelegate>


@property (nonatomic, strong)SegmentTapView *segment;
@property (nonatomic, strong)FlipTableView *flipView;
@property(nonatomic,strong)LKManagerAlianceListViewController *list1;
@property(nonatomic,strong)LKManagerAlianceListViewController *list2;
@property(nonatomic,strong)LKManagerAlianceListViewController *list3;
@property (nonatomic,retain) UISearchController *searchController;
@property(nonatomic,strong)LKManagerAllianceSearchViewController *searchResultVc;
@property(nonatomic,strong)UIView *searchBarView;


@property(nonatomic,copy)NSString *types;
@end

@implementation LKManagerAllianceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.types = @"1";
    [[NSUserDefaults standardUserDefaults] setObject:self.types forKey:@"shopTypeValue"];
    [self setNavgationRightBtn];
    [self createContainerVC];
    [self setUpSearchViewController];
    self.navigationItem.title = @"管理商户";
    // Do any additional setup after loading the view.
}
/**
 设置返回按钮样式
 */
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"新增商户" titleColor:COLOR_HEX(0xF86747) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 12] backgroudColor:[UIColor whiteColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        LKAddShopViewController *addVc = [[LKAddShopViewController alloc] init];
        [weakSelf.navigationController pushViewController:addVc animated:YES];
    }];
    rightBtn.layer.borderColor = COLOR_HEX(0xF86747).CGColor;
    rightBtn.layer.borderWidth = 1;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

/// 增加新商户
/// @param btn 按钮
-(void)addNewShop:(UIButton *)btn{
    LKOpenShopStepOneViewController *steponeVc = [[LKOpenShopStepOneViewController alloc] init];
    [self.navigationController pushViewController:steponeVc animated:YES];
}
-(void)createContainerVC{
    WS(weakSelf)
    self.searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 40)];
    _searchBarView.backgroundColor = viewDefualtColor;
        [self.view addSubview:_searchBarView];
        self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT + 40, WIDTH, 42) withDataArray:[NSArray arrayWithObjects:@"待处理",@"待提交资料",@"总部审核", nil] withFont:14];
        self.segment.delegate = self;
        self.segment.lineColor = yellowDefaultColor;
        self.segment.titleFont = 14;
        self.segment.textNomalColor = COLOR_HEX(0x333333);
        self.segment.textSelectedColor = yellowDefaultColor;
        [self.view addSubview:self.segment];
        self.list1 = [[LKManagerAlianceListViewController alloc] init];
        _list1.pushBlock = ^(LkMyShopOpenStatusListModel *statusModel) {
            if ([statusModel.agent_type_id isEqualToString:@"2"]) {
                LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
                allTrendsVc.mid = statusModel.m_id;
                [weakSelf.navigationController pushViewController:allTrendsVc animated:YES];
            }else{
                LKOpenShopInforDataViewController *vc = [[LKOpenShopInforDataViewController alloc] init];
                vc.uid = statusModel.Id;
                vc.showBottomBtn = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        };
        _list1.shopOpenStatu = MyShopOpenStatuOpenWaiting;
        self.list2 = [[LKManagerAlianceListViewController alloc] init];
        _list2.pushBlock = ^(LkMyShopOpenStatusListModel *statusModel) {
            if ([statusModel.agent_type_id isEqualToString:@"2"]) {
                LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
                allTrendsVc.mid = statusModel.m_id;
                [weakSelf.navigationController pushViewController:allTrendsVc animated:YES];
            }else{
                LKOpenShopInforDataViewController *vc = [[LKOpenShopInforDataViewController alloc] init];
                vc.uid = statusModel.Id;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }

        };
        _list2.shopOpenStatu = MyShopOpenStatuCommitData;
        self.list3 = [[LKManagerAlianceListViewController alloc] init];
        _list3.pushBlock = ^(LkMyShopOpenStatusListModel *statusModel) {
            LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
            allTrendsVc.mid = statusModel.Id;
            
            [weakSelf.navigationController pushViewController:allTrendsVc animated:YES];
        };
        _list3.shopOpenStatu = MyShopOpenStatuCheck;
        self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 85, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 85)) withArray:@[_list1,_list2,_list3]];
        self.flipView.delegate = self;
        [self.view addSubview:self.flipView];
}
#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
//    NSLog(@"%ld",index);
    [self.flipView selectIndex:index];
    self.types = [NSString stringWithFormat:@"%ld",index + 1];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"shopTypeValueChanged" object:@{@"types":self.types}];
    [[NSUserDefaults standardUserDefaults] setObject:self.types forKey:@"shopTypeValue"];
    NSLog(@"%@",self.types);
}

-(void)scrollChangeToIndex:(NSInteger)index
{
//    NSLog(@"%ld",index);
    [self.segment selectIndex:index];
    self.types = [NSString stringWithFormat:@"%ld",index];
    [[NSUserDefaults standardUserDefaults] setObject:self.types forKey:@"shopTypeValue"];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"shopTypeValueChanged" object:@{@"types":self.types}];
    NSLog(@"%@",self.types);
}

- (UIView *)findViewWithClassName:(NSString *)className inView:(UIView *)view{
    Class specificView = NSClassFromString(className);
    if ([view isKindOfClass:specificView]) {
        return view;
    }
    
    if (view.subviews.count > 0) {
        for (UIView *subView in view.subviews) {
            UIView *targetView = [self findViewWithClassName:className inView:subView];
            if (targetView != nil) {
                return targetView;
            }
        }
    }
    
    return nil;
}

/// 设置搜索控制器
-(void)setUpSearchViewController{
        //创建UISearchController
        self.searchResultVc = [[LKManagerAllianceSearchViewController alloc] init];
    self.searchResultVc.type = self.types;
        UINavigationController *navSearchVc = [[UINavigationController alloc] initWithRootViewController:self.searchResultVc];
        self.searchController = [[UISearchController alloc]initWithSearchResultsController:navSearchVc];
        //设置代理
        self.searchController.delegate= self.searchResultVc;
        self.searchController.searchResultsUpdater = self.searchResultVc;
        //包着搜索框外层的颜色
        self.searchController.searchBar.barTintColor = viewDefualtColor;
        //提醒字眼
        self.searchController.searchBar.placeholder= @"可搜索店铺";
        //设置UISearchController的显示属性，以下3个属性默认为YES
        //搜索时，背景变暗色
        self.searchController.dimsBackgroundDuringPresentation = YES;
        //搜索时，背景变模糊
        self.searchController.obscuresBackgroundDuringPresentation = YES;
        
        //点击搜索的时候,是否隐藏导航栏
        self.searchController.hidesNavigationBarDuringPresentation = YES;
    [self.searchController.searchBar sizeToFit];
        //位置
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 40.0);
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchController.view.backgroundColor = [UIColor whiteColor];
    [self.searchBarView addSubview:self.searchController.searchBar];
        #warning 如果进入预编辑状态,searchBar消失(UISearchController套到TabBarController可能会出现这个情况),请添加下边这句话
    self.definesPresentationContext=YES;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
